package com.jyw.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jyw.R;
import com.jyw.pojo.SignData;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/13
 */

public class QianDaoAdapter extends RecyclerView.Adapter<QianDaoAdapter.QianDaoHolder> {
    private int MONTH_LENGTH[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 0-based
    private int LEAP_MONTH_LENGTH[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; // 0-based

    private List<Integer> listData = new ArrayList<>();
    private Map<Integer, Boolean> data = new LinkedHashMap<>();
    private Context mCtx;

    public QianDaoAdapter(Context ctx) {
        this.mCtx = ctx;

        Calendar cal = Calendar.getInstance();
        for (int i = getFirstSunday(); i < monthLength(cal.get(Calendar.MONTH)
                , cal.get(Calendar.YEAR) + 1); i++) {
            listData.add(i);
        }
        for (int i = 1; i < getFirstSunday(); i++) {
            listData.add(i);
        }
        for (Integer i : listData) {
            data.put(i, Boolean.FALSE);
        }
    }

    public void swapData(List<SignData> signData) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMd");
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        for (SignData s : signData) {
            try {
                Date date = sdf.parse(s.getDate());
                calendar.setTime(date);
                if (calendar.get(Calendar.YEAR) == year && calendar.get(Calendar.MONTH) == month) {
                    data.put(calendar.get(Calendar.DAY_OF_MONTH), Boolean.TRUE);
                }
            } catch (ParseException e) {
                Log.e("simonwLog", "错误：日期解析");
            }
        }
        notifyDataSetChanged();
    }

    private int getFirstSunday() {
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.DAY_OF_MONTH, 1);

        int i = 1;
        while (cal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
            cal.set(Calendar.DAY_OF_MONTH, i++);
        }

        return cal.get(Calendar.DAY_OF_MONTH);
    }

    private int monthLength(int month, int year) {
        return isLeapYear(year) ? LEAP_MONTH_LENGTH[month] : MONTH_LENGTH[month];
    }

    private boolean isLeapYear(int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    @Override
    public QianDaoHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new QianDaoHolder(LayoutInflater.from(mCtx).inflate(R.layout.item_qindao, null));
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(QianDaoHolder holder, int position) {
        holder.tv.setText(listData.get(position) + "");
        if (data.get(listData.get(position))) {
            holder.vNike.setVisibility(View.VISIBLE);
        } else {
            holder.vNike.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    class QianDaoHolder extends RecyclerView.ViewHolder {
        TextView tv;
        View vNike;

        public QianDaoHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.tv);
            vNike = itemView.findViewById(R.id.v_nike);
        }
    }
}
